<template>
  <div class="rateQuery">
   
    <a-form @submit="handleSubmit" :form="form" style="margin:0 auto;">
      <div style="width: 100%;margin:20px auto;">
        <a-form-item v-bind="formItemLayout" label="模板名称">
          <a-input
            style="width:60%"
            v-decorator="[
              'feename',
              { rules: [{ required: true, message: '请输入模板名称' }, {validator: checkTempNameValue}] }
            ]"
            placeholder="请输入模板名称"
          />
          <span class='spanName'>(模板名称支持中文，英文，数字)</span>
        </a-form-item>
  
        <mer-rate :MerRateList="MerRateList" ref="getMerRateData"></mer-rate>

        <a-form-item
          v-bind="formItemLayout"
          style="margin-top:20px;"
          label="设为默认"
        >
          <a-select
            style="width:60%"
            v-model="defaultValue"
            placeholder="设为默认"
          >
            <a-select-option value="true">是</a-select-option>
            <a-select-option value="false">否</a-select-option>
          </a-select>
        </a-form-item>
      </div>

      <div class="ratebutton">
        <a-button
          style="width:90px;margin-top:1vh;"
          html-type="submit"
          type="primary"
          :loading="loading"
          >保存</a-button
        >
        <!-- <a-button
          style="width:90px;margin-top:1vh;margin-left:20px;"
          @click="goback"
          type="primary"
        >取消</a-button>-->
      </div>
    </a-form>
  </div>
</template>
<script>
import AHideData from "../../../common/HideData";
import MerRate from "../../../common/MerRate";
import api from "@/common/api";
const columns = [
  { title: "功能名称", dataIndex: "name" },
  { title: "费率", scopedSlots: { customRender: "feeTemplate" } },
  { title: "当前状态", scopedSlots: { customRender: "status" } }
];
const columns2 = [
  { title: "发卡行", dataIndex: "bankName" },
  { title: "卡类型", scopedSlots: { customRender: "cardType" } },
  { title: "费率", scopedSlots: { customRender: "feeTemplate" } },
  { title: "封顶金额", scopedSlots: { customRender: "transAmountTop" } }
];

export default {
  components: {
    AHideData,
    MerRate
  },
  data() {
    return {
      ratelist: [],
      templateBankDTOList: [],
      fileList: [],
      transStatus: [],
      MerRateList:null,
      columns,
      columns2,
      query: {},
      feeType: "请选择",
      defaultValue: "true",
      feeName: "",
      visible: false,
      loading: false,
      issend: true,
      previewVisible: false,
      previewImage: "",
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
          md: { span: 16 }
        }
      }
    };
  },
  mounted() {
    //   this.spinning = true;
    this.getratelist();
    
  },

  methods: {

    checkTempNameValue (rule ,value ,callback) {
      var flag = new RegExp("[`/&]")
      if (value && flag.test(value)) {
        callback("名字不能包含特殊字符'/&'等")
      }

      callback()

    },

    goback() {
      this.$router.push("/org/transmanage/rateTemplate");
    },
    getratelist() {
      this.$axios
        .get("/agent/fee/group/getDefaultOfflineMerchantFee")
        .then(res => {
          console.log("rate", res);
          if (res.data.code == "OK") {
            this.$refs.getMerRateData.newfee = true
            this.MerRateList = res.data.data
            
          } else {
            // this.spinning=false;
          }
        });
    },
    onChange2(e) {
      console.log("radio checked", e.target.value);
      console.log("radio checked", e.target.name);
      let index = Number(e.target.name);
      if (e.target.value == "true") {
        this.ratelist[index].open = true;
      } else {
        this.ratelist[index].open = false;
      }
    },
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
      
          this.$refs.getMerRateData.rateConversion()

          if(this.$refs.getMerRateData.feeTemplateList.length == 0){
            console.log("数组为0")
            return false
          }
          // console.log("通过",this.$refs.getMerRateData.feeTemplateList)
          let feeTemplateAddRequestList = this.$refs.getMerRateData.feeTemplateList

          if (this.defaultValue == "true") {
            this.defaultValue = true;
          } else {   
            if(localStorage.getItem("ifTemplate") == "true"){
              this.$message.warning("请设置默认模板");
              return false
            }
            this.defaultValue = false;
          }
          console.log(feeTemplateAddRequestList + "");
          console.log(this.defaultValue);
          this.loading = true;
          this.$axios
            .post("/agent/fee/group/addFeeTemplateGroup", {
              code: "offline_merchant",
              name: values.feename,
              defaultValue: this.defaultValue,
              feeTemplateAddRequestList: feeTemplateAddRequestList
            })
            .then(
              res => {
                this.loading = false;
                console.log(res);
                if (res.data.code == "OK") {
                  this.$router.push("/org/transmanage/rateTemplate");
                } else {
                  this.$message.warning(res.data.message);
                }
              },
              err => {
                this.loading = false;
                this.$message.error(err.message);
              }
            );
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
/deep/.noExpand {
  .ant-table-row-expand-icon {
    display: none;
  }
}
.spanName {
  font-size: 10px;
  color: #ababab;
  margin-left: 10px;
}
.rateQuery {
  overflow: scroll;
  height: 100%;
  /* padding: 30px; */
  overflow-x: hidden;
  overflow-y: auto;
}
/*滚动条样式*/
.rateQuery::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 4px;
  /*高宽分别对应横竖滚动条的尺寸*/
  height: 4px;
}

.rateQuery::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 5px;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}

.rateQuery::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
.ratebutton {
  text-align: center;
}
</style>
